//
//  TTAutoEstimateDiamondListView.m
//  TaoTuo
//
//  Created by LG on 2020/8/15.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTAutoEstimateDiamondListView.h"
#import "TTPassDiamondInfoViewCell.h"

static NSString *cellID = @"cell";

@interface TTAutoEstimateDiamondListView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<NSDictionary *> *dataArray;
@property (nonatomic, copy) NSString *certString;

@end

@implementation TTAutoEstimateDiamondListView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 8;
        
        UILabel *infoLabel = [UILabel labelWithTitle:@"钻石信息" font:[UIFont boldSystemFontOfSize:15]];
        [self addSubview:infoLabel];
        [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.left.mas_equalTo(18);
            make.height.mas_equalTo(50);
        }];
        
        _lookZSBtn = [UIButton createButtonWithTitle:@"预览证书" titleColor:UIColorHex(#9444EB) titleFont:[UIFont systemFontOfSize:13] backgroundColor:nil target:self action:@selector(lookZsBtnClick)];
//        _lookZSBtn.hidden = YES;
        [self addSubview:_lookZSBtn];
        [_lookZSBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).mas_offset(-10);
            make.centerY.equalTo(infoLabel);
        }];
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        _tableView.rowHeight = 30;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        [_tableView registerClass:[TTPassDiamondInfoViewCell class] forCellReuseIdentifier:cellID];
        _tableView.layer.cornerRadius = 8;
        [self addSubview:_tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(infoLabel.mas_bottom);
            make.left.right.equalTo(self);
            make.height.mas_equalTo(200);
        }];
        
        
    }
    return self;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTPassDiamondInfoViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.dic = self.dataArray[indexPath.row];
    return cell;
}


/// 查看证书
- (void)lookZsBtnClick
{
    [TTLoginSingle lookCertificateWithUrlString:self.certString];
    if (self.clickLookZsBlock) {
        self.clickLookZsBlock();
    }
}

- (void)setDataArray:(NSMutableArray *)dataArray certUrlString:(NSString *)certUrlString
{
//    self.dataArray = dataArray;
    self.certString = certUrlString;
    
    [self.dataArray removeAllObjects];
    
    [self layoutIfNeeded];
    
    for (NSDictionary *dic in dataArray) {
        
        NSMutableDictionary *mDic = dic.mutableCopy;
        
        NSString *leftStr = mDic[@"leftTitle"];
        NSString *rightStr = mDic[@"rightTitle"];
        // 计算宽度
        CGFloat leftW = [leftStr boundingRectWithSize:CGSizeMake(MAXFLOAT, 24) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesDeviceMetrics attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:11]} context:nil].size.width + 10;
        
        CGFloat rightW = [rightStr boundingRectWithSize:CGSizeMake(MAXFLOAT, 24) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesDeviceMetrics attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:11]} context:nil].size.width + 10;
        
        mDic[@"leftW"] = @(leftW);
        mDic[@"rightW"] = @(rightW);
        
        mDic[@"lineW"] = @(self.width - 35 - leftW - rightW - 20);
        
        [self.dataArray addObject:mDic];
        
    }
    
    [self.tableView reloadData];
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30 * dataArray.count);
    }];
//    self.height = 30 * dataArray.count + 50;
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30 * dataArray.count + 50);
    }];
    
}

- (NSMutableArray<NSDictionary *> *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
